/*
 * Copyright 2009 Gordon Turner
 * 
 * This file is part of openwiimote.
 *
 * openwiimote is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * openwiimote is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public License
 * along with Wiimote Simple.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.openwiimote.examples;
import java.util.ArrayList;
import java.util.Iterator;

import org.apache.log4j.Logger;
import org.openwiimote.Wiimote;
import org.openwiimote.WiimoteDiscoverer;
import org.openwiimote.listeners.WiimoteDiscoveryListenerImpl;
import org.openwiimote.listeners.WiimoteListenerImpl;


/**
 * @author Gordon Turner
 *
 */
public class WiimoteSimpleVibrate
{
  static Logger logger = Logger.getLogger( WiimoteSimpleVibrate.class );

  static WiimoteDiscoveryListenerImpl wiimoteDiscoveryListener;

  static WiimoteDiscoverer wiimoteDiscoverer;

  static Thread wiimoteDiscovererThread;

  public static Boolean stopRequest = new Boolean( false );

  /**
   * @param args
   */
  public static void main( String args[] )
  {
    logger.debug( "BEGIN" );
    logger.debug( Thread.currentThread().getName() );

    int maxWiimotes = 1;

    /*
     * Set "bluecove.jsr82.psm_minimum_off" to "true" to prevent:
     * 
     *  java.lang.IllegalArgumentException: 
     *  PCM values restricted by JAR82 to minimum 4097
     */
    System.setProperty( "bluecove.jsr82.psm_minimum_off", "true" );

    wiimoteDiscoverer = WiimoteDiscoverer.getWiimoteDiscoverer();
    wiimoteDiscoverer.setName( "WiimoteDiscovererThread" );
    wiimoteDiscoveryListener = new WiimoteDiscoveryListenerImpl();
    wiimoteDiscoverer.addWiimoteDiscoveryListener( wiimoteDiscoveryListener );
    wiimoteDiscoverer.start();

    while( wiimoteDiscoverer.isAlive() )
    {
      if( wiimoteDiscoveryListener.wiimotes.size() >= maxWiimotes )
      {
        wiimoteDiscoverer.stopWiimoteSearch();
      }
    }

    ArrayList wiimotes = (ArrayList) wiimoteDiscoveryListener.wiimotes;

    Iterator iter;
    Wiimote wiimote;

    WiimoteListenerImpl wiimoteListener = new WiimoteListenerImpl();

    logger.debug( "Doing something with the Wiimotes." );
    iter = wiimotes.iterator();
    while( iter.hasNext() )
    {
      wiimote = (Wiimote) iter.next();
      wiimote.setLight( 0 );

      wiimote.vibrate( 1000 );

      wiimote.addListener( wiimoteListener );
    }

    /*
     * We have to sleep to give the vibrate call a chance to execute.
     * 
     * 2 seconds should be enough for most machines.
     */
    try
    {
      Thread.sleep( 2000 );
    }
    catch( InterruptedException e )
    {
    }
    
    logger.debug( "Cleaning up Wiimotes." );
    iter = wiimotes.iterator();
    while( iter.hasNext() )
    {
      wiimote = (Wiimote) iter.next();
      wiimote.cleanup();
    }

    logger.debug( "END" );
  }

}